package com.labreeze.spring.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Test01 {
	
	private DriverManagerDataSource ds=null;
	private JdbcTemplate jdbcTemplate=null;
	
	public Test01(){
		ds=new DriverManagerDataSource();
		ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		ds.setUrl("jdbc:oracle:thin:@10.10.10.127:1521:oel127b");
		ds.setUsername("id");
		ds.setPassword("id");
		jdbcTemplate=new JdbcTemplate();
		jdbcTemplate.setDataSource(ds);
	}
	
	public static void main(String[] args) {
		
		Test01 test=new Test01();
		
		test.query2();
		
	}
	
	public void createTable(){
		String sql="create table tt_user(id number(10) primary key , name varchar2(100)) ";
		jdbcTemplate.execute(sql);
	} 
	
	public void insert(){
		String sql="insert into tt_user(id,name) values(?,?)";
		Object[] params=new Object[]{1,"test"};
		jdbcTemplate.update(sql, params);
	}
	
	public void insert2(){
		String sql="insert into tt_user(id,name) values(?,?)";
		final int id=2; final String name="test2";
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			
			public void setValues(PreparedStatement ps) throws SQLException {
				// TODO Auto-generated method stub
				ps.setInt(1, id);
				ps.setString(2, name);
			}
		});
	}
	
	
	public void query(){
		String sql ="select * from tt_user where id=?";
		jdbcTemplate.query(sql, new Object[]{1}, new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				System.out.println("id:"+rs.getInt("id"));
				System.out.println("name:"+rs.getString("name"));
			}
		});
	}
	
	public void query2(){
		String sql ="select * from tt_user";
		jdbcTemplate.query(sql,  new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				System.out.println("id:"+rs.getInt("id"));
				System.out.println("name:"+rs.getString("name"));
			}
		});
		
		
	}
	

}
